package com.tomcov.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tomcov.client.service.MessageService;
import com.tomcov.shared.Message;

public class MessageServiceImpl extends RemoteServiceServlet implements MessageService{

	private static final long serialVersionUID = 1L;

	@Override
	public Message AddMessage(Message message) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
            pm.makePersistent(message);
            return message;
        } finally {
            pm.close();
        }
	}

	@Override
	public List<Message> getMessageTo(String User) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query= pm.newQuery(com.tomcov.shared.Message.class);
		query.setFilter("To == userNameParam");
		query.declareParameters("String userNameParam");
		try{
			@SuppressWarnings("unchecked")
			List<Message> results = (List<Message>) query.execute(User);
				return results;
			}
		finally{
			query.closeAll();
			pm.close();
		}
	}

	@Override
	public List<Message> getMessageFrom(String User) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query= pm.newQuery(com.tomcov.shared.Message.class);
		query.setFilter("From == userNameParam");
		query.declareParameters("String userNameParam");
		try{
			@SuppressWarnings("unchecked")
			List<Message> results = (List<Message>) query.execute(User);
				return results;
			}
		finally{
			query.closeAll();
			pm.close();
		}
	}

}
